package com.charging.business.enums;

import org.springframework.stereotype.Component;

/**
 * @author JerryLu
 * @date 2024/8/26 20:14
 * @description 免费类型
 */
@Component
public class Free {
    /**
     * 军车
     */
    public static final int ARMY = 0;
    /**
     * 本省警车
     */
    public static final int POLIC = 1;
    /**
     * 紧急车
     */
    public static final int JJ = 2;
    /**
     * 抢险救灾
     */
    public static final int QXJZ = 3;
    /**
     * 应急救援车
     */
    public static final int ERES = 4;
    /**
     * 部疫苗车
     */
    public static final int VACC = 5;
    /**
     * 部抢险救灾
     */
    public static final int MQXJY = 6;
    /**
     * 绿通车
     */
    public static final int GREEN = 8;
    /**
     * 联合收割机
     */
    public static final int COMHV = 9;
    /**
     * 节假日免费
     */
    public static final int HOLIDAY = 10;
    /**
     * 免费U行
     */
    public static final int UPASS = 11;
    /**
     * 点对政府买单
     */
    public static final int P2PGOV = 12;

    int type = 0;

    public Free() {
        type = 0;
    }

    public static boolean tst32bit(int src, int... bits) {
        long aa = Integer.toUnsignedLong(src);
        long bb = Integer.toUnsignedLong(set32bit(0, bits));
        return (aa & bb) != 0;
    }

    public static int set32bit(int src, int... bits) {
        long aa = Integer.toUnsignedLong(src);
        for (int bit : bits) {
            if (bit >= 0 && bit <= 31) {
                long bb = Integer.toUnsignedLong(1 << bit);
                aa = aa | bb;
            }
        }
        return (int) aa;
    }

    /**
     * 设置免费类型
     *
     * @param bits - 免费位
     * @return
     */
    public int set(int... bits) {
        type = set32bit(type, bits);

        return type;
    }

    /**
     * 获取免费类型
     */
    public String getInfo() {
        String ret = "";
        if (test(ARMY)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "军车";
        }
        if (test(POLIC)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "本省警车";
        }
        if (test(JJ)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "紧急";
        }
        if (test(QXJZ)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "抢险救灾";
        }
        if (test(ERES)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "应急救援";
        }
        if (test(VACC)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "部疫苗车";
        }
        if (test(MQXJY)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "部抢险救灾";
        }
        if (test(GREEN)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "绿通";
        }
        if (test(COMHV)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "联合收割";
        }
        if (test(HOLIDAY)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "节假日";
        }
        if (test(UPASS)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "免费U行";
        }
        if (test(P2PGOV)) {
            if (!ret.isEmpty()) {
                ret += "|";
            }
            ret += "点对政府买单";
        }

        return ret;
    }

    public boolean test(int... bits) {
        for (int bit : bits) {
            if (tst32bit(type, bit)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 全网免费车判断<p>
     * <b>注意：不含本省警车</b>
     * <p>
     * 军车、紧急车、抢险救灾、应急救援、节假日免费、免费U行、绿通、联合收割、疫苗车、部抢险救灾为全免车
     *
     * @param haveGreen - 是否包括绿通及联合收割机, true:包含; false:不包括
     * @return true:为全免车; false:非全免车
     */
    public boolean isAllFree(boolean haveGreen) {
        if (!haveGreen) {
            return tst(ARMY, JJ, QXJZ, ERES, HOLIDAY, UPASS, VACC, MQXJY);
        }
        return tst(ARMY, JJ, QXJZ, ERES, HOLIDAY, UPASS, VACC, MQXJY, GREEN, COMHV);
    }

    public boolean tst(int... bits) {
        for (int bit : bits) {
            if (tst32bit(type, bit)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 预约车(绿通及联合收割机)检查
     */
    public boolean isGreen() {
        return tst(GREEN, COMHV);
    }

    /**
     * 本省免费判断<p>
     * 陕西警车为本省免费车
     *
     * @return true:为本省免费车；false:非本省免费车
     */
    public boolean isLocalFree() {
        return tst(POLIC);
    }

    public boolean isFree()
    {
        return type==0?false:true;
    }

    /**
     *	获取免费类型
     * @return
     */
    public int get()
    {
        return type;
    }
}
